﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Radiation___IM.ApiCommands
{
    public class RemoveFriend: ICommand
    {
        private readonly string m_commandkey = Radiation.Common.Communication.APIMethods.REMOVE_FRIEND;

        public RemoveFriend()
        {
        }

        public string commandKey()
        {
            return m_commandkey;
        }

        public string requiredPermission()
        {
            return "manage_friends";
        }

        public void execute(string[] commands, Client client)
        {
            if (Users.UserManager.Instance[client.Nickname] != null &&
                                commands.Length > 2 &&
                                commands[2].Length > 1) // if user is logged in and input valid
            {
                if (Users.UserManager.Instance[client.Nickname].Friends.Contains(commands[2]))
                {
                    Users.UserManager.Instance[client.Nickname].Friends.Remove(commands[2]);
                    MessageExecution.sendDataAPI(client,
                        Radiation.Common.Communication.Constants.REMOVEFRIEND_SUCCESS,
                        commands[1]);
                    MessageExecution.sendDataAPI(client,
                        commands[2],
                        Radiation.Common.Communication.APIMethods.CLIENT_REMOVE_FRIEND);
                    Data.DatabaseWriter.Instance.Queue(Users.UserManager.Instance[client.Nickname].getFile());
                }
                else
                    MessageExecution.sendDataAPI(client,
                        Radiation.Common.Communication.Constants.REMOVEFRIEND_NOT_EXISTS,
                        commands[1]);
            }
            else
                MessageExecution.sendDataAPI(client,
                    Radiation.Common.Communication.Constants.REMOVEFRIEND_NOT_LOGGED_IN,
                    commands[1]);
        }
    }
}
